﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.IO;
using System.Linq;
using UnityEngine;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class Vector2Processor : GenericDataProcessor<Vector2>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Vector2";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "vector2",
                    "unityengine.vector2"
                };
            }

            public override Vector2 Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                string[] splitedValue = value.Split(',');
                return new Vector2(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                Vector2 vector2 = Parse(value);
                binaryWriter.Write(vector2.x);
                binaryWriter.Write(vector2.y);
            }
        }
        private sealed class Vector2ArrayProcessor : GenericDataArrayProcessor<Vector2>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Vector2[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "vector2[]",
                    "unityengine.vector2[]"
                };
            }

            public override Vector2 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                string[] splitedValue = value.Split(',');
                return new Vector2(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    Vector2 vector2 = array[i];
                    binaryWriter.Write(vector2.x);
                    binaryWriter.Write(vector2.y);
                }
            }
        }
        private sealed class Vector2ListProcessor : GenericDataListProcessor<Vector2>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<Vector2>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<vector2>",
                    "list<unityengine.vector2>",
                    "system.collections.generic.list<vector2>",
                    "system.collections.generic.list<unityengine.vector2>",
                };
            }

            public override Vector2 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                string[] splitedValue = value.Split(',');
                return new Vector2(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    Vector2 vector2 = array[i];
                    binaryWriter.Write(vector2.x);
                    binaryWriter.Write(vector2.y);
                }
            }
        }
        private sealed class Vector2DictionaryProcessor : GenericDataDictionaryProcessor<Vector2>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,Vector2>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,vector2>",
                    "dictionary<string,unityengine.vector2>",
                    "dictionary<system.string,Vector2>",
                    "dictionary<system.string,unityengine.vector2>",
                    "system.collections.generic.dictionary<string,vector2>",
                    "system.collections.generic.dictionary<string,unityengine.vector2",
                    "system.collections.generic.dictionary<system.string,vector2>",
                    "system.collections.generic.dictionary<system.string,unityengine.vector2>",
                };
            }

            public override Vector2 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                string[] splitedValue = value.Split(',');
                return new Vector2(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    Vector2 vector2 = item.Value;
                    binaryWriter.Write(vector2.x);
                    binaryWriter.Write(vector2.y);
                }
            }
        }
    }
}
